// pages/seconds-kill/index.js
var formatTime = require('../../utils/util.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    secondKillList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSecondKillList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },


  tabSourceClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });

  },

  /**
   * 获取秒杀商品列表
   */
  getSecondKillList: function () {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/product/seckilllist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log('getSecondKill')
        console.log(res.data)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          if (res.data.data) {
            let secondsList = res.data.data
            for (var i = 0; i < secondsList.length; i++) {
              secondsList[i].date_from = formatTime.formatTime(new Date(secondsList[i].start_at)) + '-' + formatTime.formatTime(new Date(secondsList[i].end_at))
            }


            that.setData({
              secondKillList: secondsList,
            })
          }
        }


      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
  },

  showRestTime: function (endTime) {
    let that = this
    that.data.interval = setInterval(function () {
      let endDate = new Date(formatTime.formatTime(new Date(endTime)))
      let restTime = formatTime.restTime(new Date(), endDate)
      let categoriesTemp = that.data.categories
      categoriesTemp[0].rest = restTime
      //console.log(restTime)
      that.setData({
        categories: categoriesTemp
      })

    }, 1000)
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id + "&type=1" + "&endtime=" + JSON.stringify(this.data.endTime)
    })
  },

  /**
   * 跳转至秒杀商品的列表
   */
  toSecProducts: function (e) {
    let index = e.currentTarget.dataset.index
    //console.log(this.data.secondKillList[index].seckill_product)
    //console.log(JSON.stringify(this.data.secondKillList[index].seckill_product))
    if (this.data.secondKillList[index].now == 1) {
      wx.navigateTo({
        url: "/pages/seconds-kill-products/index?index=" + index + "&type=1"
      })
    } else {
      wx.navigateTo({
        url: "/pages/seconds-kill-products/index?index=" + index + "&type=0"
      })
    }
   
  }
})